/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/**
 *
 * @description 修正补充菜单相关，兼容新零售与中邮等项目
 * @param {*} menus
 * @param {*} opts
 * @returns
 */
export function fixMenusByHost(menus, opts) {
  const {
    key,
    deleteMenu = [],
    deleteParentMenu = [],
    subKey,
    fakeAccount,
    realAccount,
    zoneDelete = [],
    area_ids,
    gp_area_ids = [],
  } = opts || {};
  const isZyAccount = key === 'post';
  const isYjy = key === 'yjy';
  const isYz = key === 'yz';
  const hasGpAuth = gp_area_ids.length > 0; // 开启共配管理权限
  menus.forEach(val => {
    const child = val.children || [];
    if (child.length > 0) {
      val.children = child.map(item => {
        // 非公司层级片区，隐藏相关菜单
        if (
          isYz &&
          zoneDelete &&
          zoneDelete.length > 0 &&
          zoneDelete.includes(item.path) &&
          area_ids &&
          area_ids !== '*'
        ) {
          if (item.path.includes('Allocation')) {
            item.hideInMenu = hasGpAuth;
          } else {
            item.hideInMenu = true;
          }
        }
        return {
          ...item,
          name: item[`name_${subKey}`] || item[`name_${key}`] || item.name,
        };
      });
    }

    // 父级菜单隐藏
    if (deleteParentMenu.includes(val.name)) {
      val.hideInMenu = true;
    }
    if (['/post', '/Allocation', '/system', '/order', '/workOrder'].includes(val.path)) {
      if (val.path == '/post') {
        isZyAccount && (val.name = '中邮驿站');
        subKey == 'td' && (val.name = '驿站管理');
      }
      if (val.path == '/workOrder') {
        isZyAccount && (val.hideInMenu = true);
      }
      if (val.path == '/Allocation') {
        if (isYjy) {
          val.name = '驿加易共配';
        } else if (isZyAccount) {
          val.name = '中邮驿路';
        }
        // 假账号屏蔽菜单
        if (fakeAccount && realAccount) {
          const deleteMenus = [
            '/Allocation/DataStatistics',
            '/Allocation/Dispat',
            '/Allocation/DataMonitor',
            '/Allocation/OperatorKanban',
            '/Allocation/CustomScan',
            '/Allocation/PrintKey',
          ];
          child.forEach((i, idx) => {
            if (deleteMenus.includes(i.path)) {
              child[idx].hideInMenu = true;
            }
          });
        }
      }
      child.forEach((i, idx) => {
        if (deleteMenu.includes(i.path)) {
          child.splice(idx, 1);
        }
      });
      val.children = child;
    }
  });

  return menus;
}
